/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.Client;
import ejb.ClientFacadeRemote;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Jean-Michel
 */
public class ListeClient extends HttpServlet {

    @EJB
    private ClientFacadeRemote cltfac;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Liste clients</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet NewServlet at " + request.getContextPath() + "</h1>");
            out.println("<h1>Liste des clients</h1>");
            out.println("<table border=\"1\">"
                    + "    <tr align=\"left\">\n"
                    + "    <th>id</th>\n"
                    + "        <th>nom</th>\n"
                    + "        <th>prenom</th>\n"
                    + "        <th>adresse</th>\n"
                    + "        <th>date naissance</th>\n"
                    + "        <th>date creation</th>\n"
                    + "        <th>nb compte</th>\n"
                    + "        <th>Consulter client</th>\n"
                    + "    </tr>");
//            Client e = new Client(1, "o", "olol", "aze", new Date("07/02/1988"),new Date("07/02/1988"),1);
//            clt.create(e);
            List clts = cltfac.findAll();
            Iterator it = clts.iterator();

            while (it.hasNext()) {
                Client c = (Client) it.next();
                out.println("<tr>");
                out.println("<td>" + c.getId() + "</td>");
                out.println("<td>" + c.getNom() + "</td>");
                out.println("<td>" + c.getPrenom() + "</td>");
                out.println("<td>" + c.getAdresse() + "</td>");
                out.println("<td>" + c.getDatenaiss() + "</td>");
                out.println("<td>" + c.getDatecrea() + "</td>");
                out.println("<td>" + c.getNbcompte() + "</td>");
                out.println("<td><a href='ListeCompte?i="+String.valueOf(c.getId())+"'>Consulter Compte</a></td>");
                out.println("</tr>");
            }
            out.println("</table>");
            out.println("<a href='AjClient'>ajout client</a>");
            out.println("</body>");
            out.println("</html>");

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
